package com.server.core;

import com.server.kit.PathKit;

public class ServerFactory {

    public static IServer getServer(String webAppDir, int port, String context, int scanIntervalSeconds) {
        return new JettyServer(webAppDir, port, context, scanIntervalSeconds);
    }

    public static IServer getServer(String webAppDir, int port, String context) {
        return getServer(webAppDir, port, context, 5);
    }

    public static IServer getServer(int port, String context, int scanIntervalSeconds) {
        return getServer(detectWebAppDir(), port, context, scanIntervalSeconds);
    }

    public static IServer getServer(int port, String context) {
        return getServer(detectWebAppDir(), port, context, 5);
    }

    public static IServer getServer(int port) {
        return getServer(detectWebAppDir(), port, "/", 5);
    }

    public static IServer getServer() {
        return getServer(detectWebAppDir(), 80, "/", 5);
    }

    private static String detectWebAppDir() {
        String rootClassPath = PathKit.getRootClassPath();
        String[] temp = null;
        if (rootClassPath.indexOf("\\WEB-INF\\") != -1) {
            temp = rootClassPath.split("\\\\");
        } else if (rootClassPath.indexOf("/WEB-INF/") != -1) {
            temp = rootClassPath.split("/");
        } else
            throw new RuntimeException("WEB-INF directory not found.");
        return temp[(temp.length - 3)];
    }
}
